package com.baimawangzi.recovery.dao.impl;

import com.baimawangzi.recovery.bean.User;
import com.baimawangzi.recovery.dao.UserDao;
import com.baimawangzi.recovery.util.dbutil.BeanHandler;
import com.baimawangzi.recovery.util.dbutil.OnlyHandler;

import java.util.List;

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {
    public UserDaoImpl() {
        clazz= User.class;
        init();
    }

    @Override
    public List<User> map(String sql, Object[] obj) {
        List<User> admins = queryRunner.query(sql, new BeanHandler<User>(clazz), obj);
        return admins;
    }

    @Override
    public List<User> findByLimit(int m, int n) {
        String sql="select id,username,password,nickname,gender,birthday,phonenum,integral from t_user limit ?,?";
        Object[] object= {m, n};
        List<User> users = queryRunner.query(sql,new BeanHandler<>(User.class),object);
        return users;
    }

    @Override
    public long getCount() {
        String sql="select count(*) totalSize from t_user where ?=?";

        Object[] objects={1,1};
        List<Object> onlylist = queryRunner.query(sql, new OnlyHandler(), objects);
        return (long) onlylist.get(0);

    }

    @Override
    public int add(User user) {
        //(null,'11111',md5('123456'),'hhhhhhhh','男',date('2001-07-10'),18044223232,100);
        String sql="insert into t_user values (null,?,md5(?),?,?,date(?),?,?,?)";
        Object[] obj={user.getUsername(),user.getPassword(),user.getNickname(),user.getGender(),user.getBirthday(),user.getPhonenum(),user.getIntegral(),user.getIntegraltime()};
        return queryRunner.update(sql,obj);

    }

    @Override
    public int delete(int id) {
        String sql ="delete from t_user where id=?";
        Object[] obj={id};
        return queryRunner.update(sql,obj);
    }

    @Override
    public User findById(int id) {
        String sql="select id,username,password,nickname,gender,birthday,phonenum,integral from t_user where id = ?";
        Object[] object= {id};
        User user = queryRunner.query(sql,new BeanHandler<>(User.class),object).get(0);
        return user;
    }

    @Override
    public int update(User user) {

        String sql="update t_user set username=?,password=md5(?),nickname=?,gender=?,birthday=?,phonenum=? where id=?";
        Object[] object= {user.getUsername(),user.getPassword(),user.getNickname(),user.getGender(),user.getBirthday(),user.getPhonenum(),user.getId()};
        return queryRunner.update(sql,object);

    }

    @Override
    public int updateNoPwd(User user) {
        String sql="update t_user set username=?,password=?,nickname=?,gender=?,birthday=?,phonenum=? where id=?";
        Object[] object= {user.getUsername(),user.getPassword(),user.getNickname(),user.getGender(),user.getBirthday(),user.getPhonenum(),user.getId()};
        return queryRunner.update(sql,object);
    }

    @Override
    public int getId(String name) {
        String sql = "select id from t_user where username=?";
        Object[] obj={name};
        return (int)queryRunner.query(sql,new OnlyHandler(),obj).get(0);
    }

    @Override
    public List<User> getAll() {
        String sql ="select id,username,password,nickname,gender,birthday,phonenum,integral from t_user where ?=?";
        Object[] objects={1,1};
        List<User> users = queryRunner.query(sql, new BeanHandler<User>(clazz), objects);
        System.out.println("in usergetall");
        for (User user : users) {
            System.out.println(user);
        }
        return users;
    }
}
